package com.liyao.windowtest4.Utils;

import net.sf.json.JSONObject;

import java.io.*;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: windowtest4
 * @description:
 * @author: liu yan
 * @create: 2024-10-31 10:19
 */
public class test {

    public static void main(String[] args) throws InterruptedException, SocketException {
//        try {
//            Properties props = System.getProperties();
//            String ip = props.getProperty("local_ip"); // 在Windows系统中，通过设置系统属性local_ip来获取IP地址
//            if (ip!= null) {
//                System.out.println("真实本地IP地址: " + ip);
//            } else {
//                InetAddress localhost = InetAddress.getLocalHost();
//                System.out.println("默认获取的本地IP地址: " + localhost.getHostAddress());
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        try {
//            Process process = Runtime.getRuntime().exec("route print");
//            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
//            String line;
//            while ((line = reader.readLine())!= null) {
//                if (line.contains("0.0.0.0")) {
//                    String[] parts = line.split("\\s+");
//                    System.out.println("网关地址: " + parts[2]);
//                    break;
//                }
//            }
//            reader.close();
//            process.destroy();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        List<JSONObject> dataList = new ArrayList<>();
        String filePath=System.getProperty("user.dir") + File.separator + "config" + File.separator + "data.txt";
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine())!= null) {
                if (line.length() > 0) {
                    JSONObject js = JSONObject.fromObject(line);
                    dataList.add(js);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        JSONObject lastObj = dataList.stream()
                .filter(obj -> obj.getString("wrenchName").equals("CGHZ0710001"))
                .max(Comparator.comparing((JSONObject obj) -> {
                    try {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date date = sdf.parse(obj.getString("date") + " " + obj.getString("time"));
                        return date;
                    } catch (Exception e) {
                        e.printStackTrace();
                        return null;
                    }
                })).orElse(null);

        if (lastObj!= null) {
            System.out.println(Integer.parseInt(lastObj.getString("screwID")));
        }else{
            System.out.println(0);
        }
    }
}
